/**
 *
 *  读取json文件执行脚本
 */
const util = require('util')
const fs = require('fs')
let configStr = fs.readFileSync('config.json')
let config = JSON.parse(configStr)
const exec = util.promisify(require('child_process').exec)

async function runScript(path, cmd) {
    const { stdout, stderr } = await exec(cmd, { cwd: path })
    if (stdout) {
        console.log('success', stdout)
    }
    if (stderr) {
        console.error('error', stderr)
    } 
}

// 执行命令
async function runProject(path) {
    for (const cmd of config.scripts) {
        await runScript(path, cmd)
    } 
}

async function start() {
    console.log('----------- 任务开始 ------------')
    var funcList = config.projects.map((path) => {
        return async () => {
            return runProject(path)
        }
    })

    for (const itemFn of funcList) {
        await itemFn()
    }

    console.log('----------- 任务结束 ------------')
}



start()
